#include "Services_QG.h"

extern void LCD_actualiza(void);

extern uint8 u8RxBuffer[RX_BUFFER_SIZE];
extern uint8 u8RxBuffPtr;
extern uint8 u8GPS_Status;
extern uint8 u8GPS_Checksum;

const uint8 GPS_VALID_GGA[] = { "GPGGA," };
const uint8 GPS_VALID_VTG[] = { "GPVTG," };


#pragma DATA_SEG MY_ZEROPAGE
uint8 u8RxTemp;
uint8 u8IsRxValid;
uint8 u8GPS_ChksmTemp;
uint8 u8GPS_TerminationCounter;

void vfnICS_Configure (void)
{
    ICSC1 = ICS_CONTROL_REG1;
    ICSC2 = ICS_CONTROL_REG2;
    
    ICSTRM =0xA6;/* NVICSTRM;                    ************************/
    ICSSC_FTRIM =0x01;/* NVFTRIM_FTRIM;          ************************/
}

void vfnSystemOptionsInit (void)
{
    SOPT1 = SYS_CONFIG_1;
    SOPT2 = SYS_CONFIG_2;      
}

interrupt 15 void isrSCI_Rx (void)
{
    (void)u8SCI_RxPending();   
    u8RxTemp = u8SCI_ReadByte();
        
    if(u8RxTemp == '$')
    {
        vfnMTIM_Reset ();
        vfnMTIM_SetStatus (MTIM_ACTIVE);
        
        // APAGAR I2C 
        // FALTA ESTO 
        
        // Reset the Rx buffer pointer temporarily to validate incoming Rx 
        u8RxBuffPtr = 0;
        
        // Initialize a flag to check which message is being received 
        u8IsRxValid = 0x11; 
               
        while(u8RxBuffPtr < 6)
        {     
            while(!u8SCI_RxPending() && !u8MTIM_CheckOvFlag());
                  
            if(u8SCI_RxPending())
            {
                vfnMTIM_Reset ();
                
                u8RxTemp = u8SCI_ReadByte ();
                
                if(u8RxTemp != GPS_VALID_GGA[u8RxBuffPtr])
                {
                    u8IsRxValid &= 0xFE;    
                }
                if(u8RxTemp != GPS_VALID_VTG[u8RxBuffPtr])
                {
                    u8IsRxValid &= 0xEF;
                }
                               
                if(!u8IsRxValid)
                {
                    // AQUI HAY QUE RECONFIGURAR LAS COSAS A COMO ESTABAN 
                    return;
                }
                u8RxBuffPtr++;
                
            }
            else      // if we left the loop because of a timeout, return
            {
                // AQUI HAY QUE RECONFIGURAR LAS COSAS A COMO ESTABAN 
                vfnMTIM_SetStatus (MTIM_INACTIVE);
                return;
            }
        } 
       
        
        u8RxBuffPtr = 0;
        
        if(u8IsRxValid & 0x01) // If message being received is GGA
        {
            u8GPS_Checksum = GPS_START_GGA_CHECKSUM;    
        }
        else                   // If message being received is VTG
        {
            u8GPS_Checksum = GPS_START_VTG_CHECKSUM;   
        }
        
        while(u8RxTemp != '*')
        { 
            while(!u8SCI_RxPending() && !u8MTIM_CheckOvFlag());
                  
            if(u8SCI_RxPending())
            {
                vfnMTIM_Reset ();
                
                u8RxTemp = u8SCI_ReadByte();
                                
                u8GPS_Checksum ^= u8RxTemp;
                
                if(u8RxBuffPtr < RX_BUFFER_SIZE) // In case gps stream is too long 
                {
                    u8RxBuffer[u8RxBuffPtr] = u8RxTemp - 0x30;
                }
                               
                u8RxBuffPtr++;   

            }
            else    // if we left the loop because of a timeout, return
            {
                // AQUI HAY QUE RECONFIGURAR LAS COSAS A COMO ESTABAN /
                vfnMTIM_SetStatus (MTIM_INACTIVE);
                return;
            }
        }
        
        // Now we read the checksum and the line termination characters
        
        u8GPS_TerminationCounter = 0;
        
        while (u8GPS_TerminationCounter < 4)
        {
            while(!u8SCI_RxPending() && !u8MTIM_CheckOvFlag());

            if(u8SCI_RxPending())
            {
                vfnMTIM_Reset ();
                
                u8RxTemp = u8SCI_ReadByte();
                
                
                if (u8GPS_TerminationCounter == 0)
                {
                    if(u8RxTemp > 0x39)
                    {
                   
                        u8GPS_ChksmTemp = (u8RxTemp - 0x37);
                        u8GPS_ChksmTemp <<= 4;
                    }
                    else
                    {
                        u8GPS_ChksmTemp = (u8RxTemp - 0x30);
                        u8GPS_ChksmTemp <<= 4; 
                    }
                }
                else if (u8GPS_TerminationCounter == 1)
                {
                    if(u8RxTemp > 0x39)
                    {
                        u8GPS_ChksmTemp += (u8RxTemp - 0x37);
                    }
                    else
                    {
                        u8GPS_ChksmTemp += (u8RxTemp - 0x30); 
                    }
                }
                
                u8GPS_TerminationCounter++;
            }
            else
            { 
                // AQUI HAY QUE RECONFIGURAR LAS COSAS A COMO ESTABAN 
                vfnMTIM_SetStatus (MTIM_INACTIVE);
                return;
            }
        }
        
    }
    else
    {
        return;
    }
 
    vfnGPS_ParseBuffer();
    vfnGPS_DistanceAproximation();
    LCD_actualiza();
    
} 

